package cn.sunshine.org.service.impl;

import cn.sunshine.basic.service.impl.BaseServiceImpl;
import cn.sunshine.basic.utils.MD5Utils;
import cn.sunshine.basic.utils.StrUtils;
import cn.sunshine.org.domain.Employee;
import cn.sunshine.org.mapper.EmployeeMapper;
import cn.sunshine.org.service.IEmployeeService;
import cn.sunshine.system.mapper.RoleMapper;
import cn.sunshine.user.domain.Logininfo;
import cn.sunshine.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
// 只读事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)


/**
 * @Description: 实现类
 * @Author: Mr.Jiang
 * @Version: 1.0
 * @Date: 2022/6/21 17:13
 */

public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    /*
     * @Title: initRelationTable
     * @Description: 员工角色数据初始化
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/7/13 17:44
     * @Parameters: [employee]
     * @Return void
     */
    public void initRelationTable(Employee employee) {
        if (employee.getRoles() != null) {
            // 根据员工的id删除关联的数据
            employeeMapper.removeRoles(employee.getId());
            // 重新 根据用户id和角色id新增角色数据
            employeeMapper.saveRoles(employee.getId(), employee.getRoles());
        }
    }

    @Override
    public void save(Employee employee) {
        // 1.初始化emplpyee，并保存，这是引用传递
        initEmployee(employee);
        employeeMapper.save(employee);
        // 2.初始化logininfo，并保存
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        // 3.将logininfo的主键Id设置到employee的外键，并修改
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.update(employee);
            // 角色添加（初始化关联表的角色数据）
            initRelationTable(employee);


    }

    /*
     * @Title: update
     * @Description: 修改
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/7/9 15:50
     * @Parameters: [employee]
     * @Return void
     */
    @Override
    public void update(Employee employee) {
        // 1.初始化emplpyee，并保存，这是引用传递
        initEmployee(employee);
        employeeMapper.update(employee);

        // 2.初始化logininfo，并保存
        Logininfo logininfo = employee2Logininfo(employee);
        //logininfo.setId(employee.getLogininfoId());
        //logininfoMapper.update(logininfo);
        logininfoMapper.save(logininfo);
        // 3.将logininfo的主键Id设置到employee的外键，并修改
        employee.setLogininfoId(logininfo.getId());
        // 角色添加（初始化关联表的角色数据）
        initRelationTable(employee);

    }

    /*
     * @Title: remove
     * @Description: 删除
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/7/9 15:50
     * @Parameters: [id]
     * @Return void
     */
    @Override
    public void remove(Long id) {

        employeeMapper.remove(id);
        // 根据员工的id删除关联的数据
        employeeMapper.removeRoles(id);


    }


    /*
     * @Title: employee2Logininfo
     * @Description: TODO
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/7/9 15:47
     * @Parameters: [employee]
     * @Return cn.sunshine.user.domain.Logininfo
     */
    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        return logininfo;

    }

    /*
     * @Title: initEmployee
     * @Description: TODO
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/7/9 15:47
     * @Parameters: [employee]
     * @Return void
     */
    private void initEmployee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String saltPassword = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setSalt(salt);
        employee.setPassword(saltPassword);
        System.out.println(employee.getPassword());
    }


}
